<template>
	<div class="chart" style="height:300px; width:100%" />
</template>

<script>
	import * as echarts from 'echarts';
	require('echarts/theme/macarons') // echarts theme
	import resize from './mixins/resize'

	export default {
		mixins: [resize],
		props: {

			chartData: {
				type: Array,
				// required: true
				default: []
			},
		},
		data() {
			return {
				chart: null
			}
		},
		watch: {
			chartData: {
				deep: true,
				handler(val) {
					this.setOptions(val)
				}
			}
		},
		mounted() {
			this.$nextTick(() => {
				this.initChart()
			})
		},
		beforeDestroy() {
			if (!this.chart) {
				return
			}
			this.chart.dispose()
			this.chart = null
		},
		methods: {
			initChart() {
				this.chart = echarts.init(this.$el, 'macarons');
				this.setOptions(this.chartData)
			},
			setOptions(chartData) {
				this.chart.setOption({
					grid: {
						left: '3%',
						right: '4%',
						bottom: '3%',
						containLabel: true
					},
					xAxis: {
            data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋"]
          },
					yAxis: {

          },
					series: [{
							type: 'bar',
              data: this.chartData
						},
					]
				})
			}
		}
	}
</script>
